package cn.imvc.app.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;

import cn.imvc.app.domains.Constants;
import cn.imvc.app.entities.Account;
import cn.imvc.app.entities.Dealer;
import cn.imvc.app.entities.DealerLevel;
import cn.imvc.app.entities.Domain;
import cn.imvc.app.entities.Member;
import cn.imvc.app.models.AccountModel;
import cn.imvc.app.models.DealerLevelModel;
import cn.imvc.app.models.DealerModel;
import cn.imvc.app.models.DomainModel;
import cn.imvc.app.models.MemberModel;
import cn.imvc.core.Render;

@Controller
@RequestMapping("/center")
public class CenterControllerImpl implements CenterController {

	@Autowired
	MemberModel memberModel;
	@Autowired
	DealerModel dealerModel;
	@Autowired
	DomainModel domainModel;
	@Autowired
	DealerLevelModel dealerLevelModel;
	
	@Autowired
	AccountModel accountModel;

	@RequestMapping("/center.html")
	public String center(String token, Model model) {
		// 用户未登录状态直接跳转登陆页面
		if (token == null || !memberModel.checkLogin(token)) {
			return "redirect:/member/login.html";
		}
		// 通过token从缓存里取member
		Member member = memberModel.getLoginMember(token);
		member = memberModel.get(member.getId());
		if (StringUtils.isEmpty(member.getName())) {
			model.addAttribute("name", member.getLoginname());
		} else {
			model.addAttribute("name", member.getName());
		}
		model.addAttribute("avatar", member.getAvatar());

		Account account = accountModel.get(member.getAccount().getId());
		model.addAttribute("points", account.getPoints());
		model.addAttribute("purse", account.getPurse());

		Dealer dealer = member.getDealer();
		if (dealer != null) {
			model.addAttribute("isdealer", true);
			model.addAttribute("dealerlevel", dealer.getDealerLevel());
			model.addAttribute("domain", Constants.DOMAIN_PREFIX + dealer.getDomain() + Constants.DOMAIN_SUFFIX);
		} else {
			model.addAttribute("isdealer", false);
		}

		// 获取能升级的level
		int level = dealer == null ? 0 : dealer.getDealerLevel().getLevel();
		List<DealerLevel> dealerLevelList = dealerLevelModel.findByLevelGreaterThan(level);
		// 个人中心是否显示我要升级
		if (dealerLevelList.size() > 0) {
			model.addAttribute("upgrade", true);
		} else {
			model.addAttribute("upgrade", false);
		}

		return "center/center";
	}

	@RequestMapping("/myqrcode.html")
	public String myqrcode(Model model,HttpServletRequest request) {
		Member member = null;

		String serverName = request.getServerName();
		String[] www = serverName.split("\\.");
		if (www.length == 3) {
			Dealer dealer = dealerModel.getByDomain(www[0]);
			if (dealer != null) {
				member = dealer.getMember();
			}
		}
		String dom = Constants.DOMAIN_PREFIX + "www" + Constants.DOMAIN_SUFFIX;
		if (member == null) {
			model.addAttribute("url", dom);
			model.addAttribute("domain", dom);
			model.addAttribute("appUrl", dom + "/app.html");
		} else {
			String url = dealerModel.updateQrcode(member);
			model.addAttribute("url", url);
			dom = Constants.DOMAIN_PREFIX + member.getDealer().getDomain() + Constants.DOMAIN_SUFFIX;
			model.addAttribute("name", member.getName());
			model.addAttribute("loginname", member.getLoginname());
			model.addAttribute("domain", dom);
			model.addAttribute("appUrl", dom + "/app.html");
		}
		return "center/myqrcode";
	}
	
	@RequestMapping("/center.htm")
	public String pccenter(String token, Model model) {
		// 用户未登录状态直接跳转登陆页面
		if (token == null || !memberModel.checkLogin(token)) {
			return "redirect:/member/login.htm";
		}
		// 通过token从缓存里取member
		Member member = memberModel.getLoginMember(token);
		member = memberModel.get(member.getId());
		model.addAttribute("name", member.getName());
		model.addAttribute("sex", member.getSex());
		model.addAttribute("avatar", member.getAvatar());
		model.addAttribute("mobile", member.getMobile());
		model.addAttribute("email", member.getEmail());
		Dealer dealer = member.getDealer();
		if (dealer != null) {
			model.addAttribute("isdealer", true);
			model.addAttribute("dealerlevel", dealer.getDealerLevel().getName());
		} else {
			model.addAttribute("isdealer", false);
			model.addAttribute("dealerlevel", "会员");
		}
		int level = dealer == null ? 0 : dealer.getDealerLevel().getLevel();
		List<DealerLevel> dealerLevelList = dealerLevelModel.findByLevelGreaterThan(level);
		if (dealerLevelList.size() > 0) {
			model.addAttribute("upgrade", true);
		} else {
			model.addAttribute("upgrade", false);
		}
		Domain domain = domainModel.findBymemberId(member.getId());
		if(domain != null){
			model.addAttribute("domain", domain.getDomain());
		}
		model.addAttribute("prefix", Constants.DOMAIN_PREFIX);
		model.addAttribute("suffix", Constants.DOMAIN_SUFFIX);
		String url=dealerModel.updateQrcode(member);
		model.addAttribute("url", url);
		return "center/pccenter";
	}
	
	@RequestMapping("/getMemberInfo.json")
	@ResponseBody
	public String getMemberInfo(String token){
		// 用户未登录状态直接跳转登陆页面
		Map<String, Object> map = new HashMap<String, Object>();
		Member member = memberModel.getLoginMember(token);
		if (member == null) {
			map.put("code", "-1");
			return Render.json(map);
		}
		member = memberModel.get(member.getId());
		if (StringUtils.isEmpty(member.getName())) {
			map.put("name", member.getLoginname());
		} else {
			map.put("name", member.getName());
		}
		map.put("avatar", member.getAvatar());
		Account account = accountModel.get(member.getAccount().getId());
		map.put("points", account.getPoints());
		map.put("purse", account.getPurse());
		map.put("teamcnt", dealerModel.getTeam(member).size());
		map.put("dealerLevelName", member.getDealer().getDealerLevel().getName());
		map.put("dealerLevelType", member.getDealer().getDealerLevel().getType());
		return Render.json(map);
	}

}
